package appfight.fileIOManager;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

public class TxtManager {

	final String Type_Str = "str_";
	final String Type_Int = "int_";
	final String Type_Float = "float_";
	final String Type_Boolean = "bool_";
	final String PartName = "data";
	Context context;
	
	public TxtManager(Context ParentContext)
	{
		context = ParentContext;
	}
	
	public String getStr(String target) throws IOException
	{
		FileInputStream  in = context.openFileInput(Type_Str+target+PartName);
		
         byte[] reader = new byte[in.available()];
         while(in.read(reader) != -1);
         String data = new String(reader);
         in.close();
         return data;
	}
	
	public void setStr(String target,String content) throws IOException
	{
		FileOutputStream fos = null;  
		fos = context.openFileOutput(Type_Str+target+PartName,Context.MODE_WORLD_WRITEABLE);
		fos.write(content.getBytes("utf-8"));
		fos.close();
	}
	
	public void setInt(String target,int content) throws IOException
	{
		FileOutputStream fos = null;  
		fos = context.openFileOutput(Type_Int+target+PartName,Context.MODE_WORLD_WRITEABLE);
		fos.write(String.valueOf(content).getBytes("utf-8"));
		fos.close();
	}
	
	public int getInt(String target) throws IOException
	{
		FileInputStream  in = context.openFileInput(Type_Int+target+PartName);
		
         byte[] reader = new byte[in.available()];
         while(in.read(reader) != -1);
         String data = new String(reader);
         in.close();
         return Integer.valueOf(data);
	}
	
	public void setFloat(String target,float content) throws IOException
	{
		FileOutputStream fos = null;  
		fos = context.openFileOutput(Type_Float+target+PartName,Context.MODE_WORLD_WRITEABLE);
		fos.write(String.valueOf(content).getBytes("utf-8"));
		fos.close();
	}
	
	public float getFloat(String target) throws IOException
	{
		FileInputStream  in = context.openFileInput(Type_Float+target+PartName);
		
         byte[] reader = new byte[in.available()];
         while(in.read(reader) != -1);
         String data = new String(reader);
         in.close();
         return Float.valueOf(data);
	}
	
	public void setBoolean(String target,boolean content) throws IOException
	{
		FileOutputStream fos = null;  
		fos = context.openFileOutput(Type_Boolean+target+PartName,Context.MODE_WORLD_WRITEABLE);
		fos.write(String.valueOf(content).getBytes("utf-8"));
		fos.close();
	}
	
	public boolean getBoolean(String target) throws IOException
	{
		FileInputStream  in = context.openFileInput(Type_Boolean+target+PartName);
		
         byte[] reader = new byte[in.available()];
         while(in.read(reader) != -1);
         String data = new String(reader);
         
         in.close();
         
         return Boolean.valueOf(data);
	}
	
}
